﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace Smith.ImageConverter
{
    public static class AppHelper
    {
        #region load setting

        public static void LoadSettings(AdvanceForm view)
        {
            LoadOutputSettings(view);
            LoadResizeSettings(view);
            LoadRenameSettings(view);
            LoadRotationSettings(view);
            LoadResampleSettings(view);
            LoadCompressionSettings(view);
        }

        static void LoadOutputSettings(AdvanceForm view)
        {
            OutputOption option = (OutputOption)Properties.Settings.Default.OutputOption;
            view.TBDirectory.Text = Properties.Settings.Default.OutputDirectory;
            view.CBDeleteSource.Checked = Properties.Settings.Default.DeleteSource;
            switch (option)
            {
                case OutputOption.InOriginal:
                    view.RBOutputInOriginal.Checked = true;
                    break;
                case OutputOption.InDirectory:
                    view.RBOutputInDirectory.Checked = true;
                    break;
            }
        }

        static void LoadResizeSettings(AdvanceForm view)
        {
            ResizeOption option = (ResizeOption)Properties.Settings.Default.ResizeOption;
            view.NWiseAdjust.Value = Properties.Settings.Default.WiseSideLength;
            view.NSideAdjust.Value = Properties.Settings.Default.SideLength;
            view.NCustomizeWidth.Value = Properties.Settings.Default.CustomizeWidth;
            view.NCustomizeHeight.Value = Properties.Settings.Default.CustomizeHeight;
            view.TBPercentageAdjust.Value = Properties.Settings.Default.ResizePercentage;
            view.CBSideAdjust.SelectedIndex = Properties.Settings.Default.SelectedSide;
            switch (option)
            {
                case ResizeOption.Original:
                    view.RBOriginalSize.Checked = true;
                    break;
                case ResizeOption.Wise:
                    view.RBWiseAdjust.Checked = true;
                    break;
                case ResizeOption.Width:
                    view.RBSideAdjust.Checked = true;                    
                    break;
                case ResizeOption.Height:
                    view.RBSideAdjust.Checked = true;
                    break;
                case ResizeOption.Customized:
                    view.RBCustomizedAdjust.Checked = true;
                    break;
                case ResizeOption.Percentage:
                    view.RBPercentageAdjust.Checked = true;
                    break;
                default:
                    break;
            }
        }

        static void LoadRenameSettings(AdvanceForm view)
        {
            RenameOption option = (RenameOption)Properties.Settings.Default.RenameOption;
            view.TxPrefix.Text = Properties.Settings.Default.RenamePrefix;
            view.TxSuffix.Text = Properties.Settings.Default.RenameSuffix;
            view.SpinSeed.Value = Properties.Settings.Default.RenameSeed;
            view.SpinDigit.Value = Properties.Settings.Default.RenameDigit;
            switch (option)
            {
                case RenameOption.Original:
                    view.RBOriginalName.Checked = true;
                    break;
                case RenameOption.Customized:
                    view.RBCustomizeNmae.Checked = true;
                    break;
            }
        }

        static void LoadRotationSettings(AdvanceForm view)
        {
            RotationFilter filter = (RotationFilter)Properties.Settings.Default.RotationFilter;
            RotationAngle angel = (RotationAngle)Properties.Settings.Default.RotationAngel;
            switch (filter)
            {
                case RotationFilter.None:
                    view.RFNone.Checked = true;
                    break;
                case RotationFilter.AllImages:
                    view.RFAll.Checked = true;
                    break;
                case RotationFilter.HorizontalImages:
                    view.RFHorizontal.Checked = true;
                    break;
                case RotationFilter.VerticalImages:
                    view.RFVertical.Checked = true;
                    break;
            }
            switch (angel)
            {
                case RotationAngle.Left90:
                    view.RALeft90.Checked = true;
                    break;
                case RotationAngle.Left180:
                    view.RALeft180.Checked = true;
                    break;
                case RotationAngle.Left270:
                    view.RALeft270.Checked = true;
                    break;
                case RotationAngle.Right90:
                    view.RARight90.Checked = true;
                    break;
                case RotationAngle.Right180:
                    view.RARight180.Checked = true;
                    break;
                case RotationAngle.Right270:
                    view.RARight270.Checked = true;
                    break;
            }
        }

        static void LoadResampleSettings(AdvanceForm view)
        {
            ResampleOption option = (ResampleOption)Properties.Settings.Default.ResampleOption;
            switch (option)
            {
                case ResampleOption.Default:
                    view.RSDefault.Checked = true;
                    break;
                case ResampleOption.Low:
                    view.RSLow.Checked = true;
                    break;
                case ResampleOption.Bilinear:
                    view.RSBilinear.Checked = true;
                    break;
                case ResampleOption.Bicubic:
                    view.RSBicubic.Checked = true;
                    break;
                case ResampleOption.NearestNeighbor:
                    view.RSNearestNeighbor.Checked = true;
                    break;
            }
        }

        static void LoadCompressionSettings(AdvanceForm view)
        {
            view.TBCompressPencentage.Value = Properties.Settings.Default.CompressQuarity;
            if (Properties.Settings.Default.DoCompress)
            {
                view.RBCompressQ.Checked = true;
            }
            else
            {
                view.RBDefaultQ.Checked = true;
            }
        } 

        #endregion

        #region save settings

        public static void SaveSettings(AdvanceForm view)
        {
            SaveOutputSettings(view);
            SaveResizeSettings(view);
            SaveRenameSettings(view);
            SaveRotationSettings(view);
            SaveResampleSettings(view);
            SaveCompressionSettings(view);
            Properties.Settings.Default.Save();
        }

        static void SaveOutputSettings(AdvanceForm view)
        {
            Properties.Settings.Default.OutputDirectory = view.TBDirectory.Text;
            Properties.Settings.Default.DeleteSource = view.CBDeleteSource.Checked;

            if (view.RBOutputInOriginal.Checked)
            {
                Properties.Settings.Default.OutputOption = (int)OutputOption.InOriginal;
            }
            else
            {
                Properties.Settings.Default.OutputOption = (int)OutputOption.InDirectory;
            }
        }

        static void SaveResizeSettings(AdvanceForm view)
        {
            Properties.Settings.Default.WiseSideLength = view.NWiseAdjust.Value;
            Properties.Settings.Default.SideLength = view.NSideAdjust.Value;
            Properties.Settings.Default.CustomizeWidth = view.NCustomizeWidth.Value;
            Properties.Settings.Default.CustomizeHeight = view.NCustomizeHeight.Value;
            Properties.Settings.Default.ResizePercentage = view.TBPercentageAdjust.Value;

            if (view.RBOriginalSize.Checked)
            {
                Properties.Settings.Default.ResizeOption = (int)ResizeOption.Original;
            }
            else if (view.RBWiseAdjust.Checked)
            {
                Properties.Settings.Default.ResizeOption = (int)ResizeOption.Wise;
            }
            else if (view.RBSideAdjust.Checked)
            {
                if (view.CBSideAdjust.SelectedIndex == 1)
                {
                    Properties.Settings.Default.ResizeOption = (int)ResizeOption.Height;
                    Properties.Settings.Default.SelectedSide = 1;
                }
                else
                {
                    Properties.Settings.Default.ResizeOption = (int)ResizeOption.Width;
                    Properties.Settings.Default.SelectedSide = 0;
                }
            }            
            else if (view.RBCustomizedAdjust.Checked)
            {
                Properties.Settings.Default.ResizeOption = (int)ResizeOption.Customized;
            }
            else
            {
                Properties.Settings.Default.ResizeOption = (int)ResizeOption.Percentage;
            }
        }

        static void SaveRenameSettings(AdvanceForm view)
        {
            Properties.Settings.Default.RenamePrefix = view.TxPrefix.Text;
            Properties.Settings.Default.RenameSuffix = view.TxSuffix.Text;
            Properties.Settings.Default.RenameSeed = view.SpinSeed.Value;
            Properties.Settings.Default.RenameDigit = view.SpinDigit.Value;
            if (view.RBOriginalName.Checked)
            {
                Properties.Settings.Default.RenameOption = (int)RenameOption.Original;
            }
            else
            {
                Properties.Settings.Default.RenameOption = (int)RenameOption.Customized;
            }
        }

        static void SaveRotationSettings(AdvanceForm view)
        {
            if (view.RFAll.Checked)
            {
                Properties.Settings.Default.RotationFilter = (int)RotationFilter.AllImages;
            }
            else if (view.RFHorizontal.Checked)
            {
                Properties.Settings.Default.RotationFilter = (int)RotationFilter.HorizontalImages;
            }
            else if (view.RFVertical.Checked)
            {
                Properties.Settings.Default.RotationFilter = (int)RotationFilter.VerticalImages;
            }
            else
            {
                Properties.Settings.Default.RotationFilter = (int)RotationFilter.None;
            }

            if (view.RALeft180.Checked)
            {
                Properties.Settings.Default.RotationAngel = (int)RotationAngle.Left180;
            }
            else if (view.RALeft270.Checked)
            {
                Properties.Settings.Default.RotationAngel = (int)RotationAngle.Left270;
            }
            else if (view.RARight90.Checked)
            {
                Properties.Settings.Default.RotationAngel = (int)RotationAngle.Right90;
            }
            else if (view.RARight180.Checked)
            {
                Properties.Settings.Default.RotationAngel = (int)RotationAngle.Right180;
            }
            else if (view.RARight270.Checked)
            {
                Properties.Settings.Default.RotationAngel = (int)RotationAngle.Right270;
            }
            else
            {
                Properties.Settings.Default.RotationAngel = (int)RotationAngle.Left90;
            }
        }

        static void SaveResampleSettings(AdvanceForm view)
        {
            if (view.RSLow.Checked)
            {
                Properties.Settings.Default.ResampleOption = (int)ResampleOption.Low;
            }
            else if (view.RSBilinear.Checked)
            {
                Properties.Settings.Default.ResampleOption = (int)ResampleOption.Bilinear;
            }
            else if (view.RSBicubic.Checked)
            {
                Properties.Settings.Default.ResampleOption = (int)ResampleOption.Bicubic;
            }
            else if (view.RSNearestNeighbor.Checked)
            {
                Properties.Settings.Default.ResampleOption = (int)ResampleOption.NearestNeighbor;
            }
            else
            {
                Properties.Settings.Default.ResampleOption = (int)ResampleOption.Default;
            }
        }

        static void SaveCompressionSettings(AdvanceForm view)
        {
            Properties.Settings.Default.CompressQuarity = view.TBCompressPencentage.Value;
            Properties.Settings.Default.DoCompress = view.RBCompressQ.Checked;
        } 

        #endregion

        #region file operations

        public static void DeleteFile(string path)
        {
            try
            {
                File.Delete(path);
            }
            catch (Exception)
            {
            }
        } 

        #endregion
    }
}
